package com.study.array;

/**
 * @author ytj
 * @date 2021/12/20
 */
public class Method4 {
    public static void main(String[] args) {
        int[] nums =new int[]{-1,0,3,5,9,12};
        int target = 2;
        int result=Method4.search(nums,target);
        System.out.println(result);
    }

    public static int search(int[] nums, int target){
        int left=0;
        int right=nums.length-1;
        int mind=0;
        while (left<=right){
            mind=(left+right)/2;
            if(nums[mind]==target){
                return mind;
            }else if(nums[mind]<target){
                left=left+1;
            }else if(target<nums[right]){
                right=right-1;
            }
        }
        return -1;
    }
}
